package com.kazalan.frontend.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.kazalan.content.document.bl.DocumentService;
import com.kazalan.domain.document.Document;
import com.kazalan.domain.document.Language;

@Controller
@SessionAttributes({ "user" })
public class DocumentController {

	private static Logger log = Logger.getLogger(DocumentController.class);
	
	private static final String DEFAULT_LANGUAGE_CODE = "en";

	@Autowired
	private DocumentService documentService;

	@RequestMapping(value = { "/documentform.html" })
	public ModelAndView documentForm(/* @ModelAttribute User user */) {

		ModelAndView modelAndView = new ModelAndView("documentform");

		modelAndView.addObject("languages", documentService.getLanguages());

		return modelAndView;
	}
	
	@RequestMapping(value = { "/adddocument.html" }, method = { RequestMethod.POST })
	public String addDocument(@RequestParam(required = true) String name,
			@RequestParam(required = false) String description,
			@RequestParam(required = false) String language) {

		Document document = new Document();
		document.setName(name);
		document.setDescription(description);

		documentService.addDocument(document);

		return "redirect:/document.html?id=" + document.getId();
	}

	@RequestMapping(value = { "/document.html" })
	public ModelAndView getDocumentById(@RequestParam String id,
			ModelMap modelMap) {

		// modelMap.addAttribute(user);
		log.debug("Get Document with Id : " + id);

		ModelAndView modelAndView = new ModelAndView("document");
		
		Document document = documentService.getDocumentById(id);
		
		if(document !=null)
			log.debug("Get Document with Id : " + document.getId() + " - " + document.getName());
		
		modelAndView.addObject("document", document);

		return modelAndView;
	}

	@RequestMapping(value = { "/savedocument.html" }, method = { RequestMethod.POST })
	public String saveDocument(@RequestParam(required = true) String id,
			@RequestParam(required = true) String name,
			@RequestParam(required = false) String description) {

		Document document = this.documentService.getDocumentById(id);
		document.setName(name);
		document.setDescription(description);

		documentService.saveDocument(document);

		return "/kazalan-frontend/document.html?id=" + document.getId();

	}
	
	@RequestMapping(value = "/sections.html", method = RequestMethod.GET)
	public @ResponseBody Children[] getSections() {

		log.debug(" ============ AJAX REQUEST ============ ");

		Children[] children = {
				new Children("Title", "key43546456", false, false),
				new Children("title 2", "key9808", false, false),
				new Children("folder", "key9808", true, true) };

		return children;
	}

	public void setContentService(DocumentService contentService) {
		this.documentService = contentService;
	}

}
